package com.skyline.wizard.mapper;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.lucene.document.Document;

import com.skyline.user.model.User;
import com.skyline.user.type.Sex;

public class UserObjMapper implements ObjMapper<User> {

	private final static String COLUMN_BIRTHDAY = "birthday";
	private final static String COLUMN_CITY = "city";
	private final static String COLUMN_COUNTRY = "country";
	private final static String COLUMN_EMAIL = "email";
	private final static String COLUMN_GRADUATE_SCHOOL = "graduateSchool";
	private final static String COLUMN_JOB = "job";
	private final static String COLUMN_NAME = "name";
	private final static String COLUMN_NICKNAME = "nickname";
	private final static String COLUMN_PORTRAIT = "portrait";
	private final static String COLUMN_PROVINCE = "province";
	private final static String COLUMN_SEX = "sex";
	private final static String COLUMN_ID = "id";

	private static UserObjMapper mapper = new UserObjMapper();

	private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	public static UserObjMapper getMapper() {
		return mapper;
	}

	public User map(Document d) {
		User u = new User();

		String birthdayStr = d.get(COLUMN_BIRTHDAY);
		if (birthdayStr != null && birthdayStr.equals("") != false) {
			Date birthdayDate;
			try {
				birthdayDate = format.parse(birthdayStr);
				u.setBirthday(birthdayDate);
			} catch (ParseException e) {
			}
		}

		u.setCity(d.get(COLUMN_CITY));
		u.setCountry(d.get(COLUMN_COUNTRY));
		u.setEmail(d.get(COLUMN_EMAIL));
		u.setGraduateSchool(d.get(COLUMN_GRADUATE_SCHOOL));
		u.setJob(d.get(COLUMN_JOB));
		u.setName(d.get(COLUMN_NAME));
		u.setNickname(d.get(COLUMN_NICKNAME));
		u.setPortrait(d.get(COLUMN_PORTRAIT));
		u.setProvince(d.get(COLUMN_PROVINCE));
		u.setSex(Sex.valueOf(d.get(COLUMN_SEX)));
		u.setId(Long.valueOf(d.get(COLUMN_ID)));

		return u;
	}
}
